package cn.shenzz.suixiang.array.leetcode_704;

class Solution {
    public static void main(String[] args) {
        int[] nums = {1, 3, 5, 7, 9, 11, 13};
        int target = 9;
        System.out.println(search(nums, target)); // Output: 4
    }


    public static int search(int[] nums, int target) {
        int n = nums.length - 1, left = 0, right = n;
        while (left <= right) {
            int mid = (left + right) >> 1;
            if (nums[mid] > target) {
                right = mid - 1;
            } else if (nums[mid] < target) {
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}